package com.osce.ex.controller;

import com.csce.domain.entity.exam.ExQuestion;
import com.csce.domain.entity.exam.vo.QuestionExcelVo;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.ex.service.tools.ExQuestionCacheService;
import com.osce.interfaces.ex.IExQuestionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 题目表Controller
 *
 * @author zzx
 * @since 2023-07-31
 */
@RestController
@RequestMapping("/ex/question")
public class ExQuestionController extends BaseController {

    @Resource
    private IExQuestionService exQuestionService;

    @Resource
    private ExQuestionCacheService cacheService;

    /**
     * 查询题目表列表
     */
    @PreAuthorize("@ss.hasPermi('ex:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExQuestion exQuestion) {
        startPage();
        List<ExQuestion> list = exQuestionService.selectExQuestionList(exQuestion);
        return getDataTable(list);
    }

    /**
     * 查询题目表列表
     */
    @PreAuthorize("@ss.hasPermi('ex:question:cache')")
    @GetMapping("/flush-cache")
    public AjaxResult flushCache() {
        cacheService.cacheQuestion();
        return success();
    }

    /**
     * 根据范畴为题目分组，获取每个范畴的题目数量
     */
    @GetMapping("/get-question-ids-group-by-topic")
    public AjaxResult getQuestionIdsGroupByTopic() {
        List<Map<Integer, Integer>> list = exQuestionService.getQuestionIdsGroupByTopic();
        return success(list);
    }

    /**
     * 导出题目表列表
     */
    @PreAuthorize("@ss.hasPermi('ex:question:export')")
    @Log(title = "题目表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExQuestion exQuestion) {
        List<ExQuestion> list = exQuestionService.selectExQuestionList(exQuestion);
        ExcelUtil<ExQuestion> util = new ExcelUtil<>(ExQuestion.class);
        util.exportExcel(response, list, "题目表数据");
    }

    /**
     * 导出模板
     */
    @PreAuthorize("@ss.hasPermi('ex:question:export')")
    @Log(title = "导出模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        List<QuestionExcelVo> list = new ArrayList<>();
        ExcelUtil<QuestionExcelVo> util = new ExcelUtil<>(QuestionExcelVo.class);
        util.exportExcel(response, list, "题目表数据");
    }

    /**
     * 导入题目表列表
     */
    @Log(title = "导入标准化病人", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ex:question:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<QuestionExcelVo> util = new ExcelUtil<>(QuestionExcelVo.class);
        List<QuestionExcelVo> questionList = util.importExcel(file.getInputStream());
        return exQuestionService.importData(questionList);
    }

    /**
     * 获取题目表详细信息
     */
    @PreAuthorize("@ss.hasPermi('ex:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(exQuestionService.selectExQuestionById(id));
    }

    /**
     * 新增题目表
     */
    @PreAuthorize("@ss.hasPermi('ex:question:add')")
    @Log(title = "题目表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExQuestion exQuestion) {
        return toAjax(exQuestionService.insertExQuestion(exQuestion));
    }

    /**
     * 修改题目表
     */
    @PreAuthorize("@ss.hasPermi('ex:question:edit')")
    @Log(title = "题目表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExQuestion exQuestion) {
        return toAjax(exQuestionService.updateExQuestion(exQuestion));
    }

    /**
     * 删除题目表
     */
    @PreAuthorize("@ss.hasPermi('ex:question:remove')")
    @Log(title = "题目表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(exQuestionService.deleteExQuestionByIds(ids));
    }
}
